package com.elves.gateway.controller;

import com.alibaba.fastjson2.JSONObject;
import com.elves.gateway.common.OkHttpUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by macro on 2019/9/25.
 */
@RestController
@Slf4j
public class GatewayController {

    @GetMapping("/fallback")
    public Object fallback() {
        Map<String, Object> result = new HashMap<>();
        result.put("data", null);
        result.put("message", "Get request fallback!");
        result.put("code", 500);
        return result;
    }


    @ResponseBody
    @GetMapping("/admin")
    public String admin(Principal principal) {
        log.info("gateway admin principal:{}", principal);
        return principal.toString();
    }

    @ResponseBody
    @GetMapping("/user")
    public String user(Principal principal) {
        log.info("gateway user principal:{}", principal);
        return principal.toString();
    }

    @ResponseBody
    @GetMapping("/vip")
    public String vip(Principal principal) {
        log.info("gateway vip principal:{}", principal);
        return principal.toString();
    }

    @ResponseBody
    @GetMapping("/me")
    public String me(Principal principal) {
        log.info("principal:{}", principal);
        return principal.toString();
    }

    @GetMapping("/see")
    public String index(@AuthenticationPrincipal Jwt jwt) {
        log.info("jwt:{}", jwt.getClaims());
        log.info("jwt:{}", jwt.getTokenValue());
        return String.format("Hello, %s!", jwt.getSubject());
    }

    @GetMapping(value = "/login/oauth2/code/oidc-client")
    @ResponseBody
    public ResponseEntity<Object> codeRegectGet(@RequestHeader HttpHeaders headers, @RequestParam String code) {
        log.info("code:{}", code);
        Map<String, List<String>> valueMap = new HashMap<String, List<String>>();
        for (String header : headers.keySet()) {
            valueMap.put(header, headers.get(header));
            log.info("header[{}]={}", header, headers.get(header));
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("code", code);
        map.put("redirect_uri", "http://app.elves.com:8000/login/oauth2/code/oidc-client");
        map.put("grant_type", "authorization_code");
        String params = map.toString();
        String response;
        try {
            Response tokenObj = OkHttpUtil.post("http://auth.elves.com:9000/oauth2/token", "oidc-client", "secret", map);
            response = tokenObj.body().string();
            log.info("token obj:{}", response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        JSONObject json = JSONObject.parseObject(response);
        return new ResponseEntity<>(json, HttpStatus.OK);
    }

}